package com.lzb.musicplayer.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.bean.PlayListBean;
import com.lzb.musicplayer.databinding.DialogAddPlaylistBinding;
import com.lzb.musicplayer.databinding.DialogDeleteBinding;
import com.lzb.musicplayer.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;


public class AddPlaylistDialog extends BaseSmartDialog<DialogAddPlaylistBinding> implements View.OnClickListener {

    public AddPlaylistDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_add_playlist;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            String name = mDataBinding.etName.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_name));
                return;
            }
            List<PlayListBean> listSp = AppSpUtils.getPlayList();
            if (listSp == null) listSp = new ArrayList<>();
            PlayListBean playListBean = new PlayListBean(name, TimeUtils.getNowMills(), new ArrayList<>());
            listSp.add(playListBean);
            AppSpUtils.savePlayList(listSp);
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}
